﻿Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports DBase.MySql
Imports DBase.scafolding.Conf
Imports Utilsb = Utils
Imports System.Web
Imports DBase.scafolding.Conf.RegulaExpression

Namespace DBase.scafolding

    Namespace Conf



        Public Class ConfigTable


            Public Enum InputType
                [Default]
                CheckBox
                DropDownList
                SearchBox
                SearchView
                [Date]
            End Enum

            Public Property Fields() As New List(Of Field)
            Public Property Editable() As Boolean
            Public Property Selectable() As Boolean
            Public Property Tablename() As String
            Public Property SqlView() As MySql.SqlQuery(Of Object)
            Public Property GridPageSize() As Integer
            Public Property GridRecord() As Integer
            Public Property Validator() As String

            Private _DB As MySql.DBContext
            <System.Xml.Serialization.XmlIgnore()> _
            Public Property DB() As MySql.DBContext
                Get
                    Return _DB
                End Get
                Set(value As MySql.DBContext)
                    _DB = value
                End Set
            End Property

            Public Property Selected() As Boolean
            Public Property EditInLine() As Boolean
            Public Property EditOutLine() As Boolean
            Public Property AddInLine() As Boolean
            Public Property AddOutLine() As Boolean

            'Public ReadOnly Property TableName As String
            '    Get
            '        If Me.Table Is Nothing Then
            '            Return ""
            '        Else
            '            Return DBContext.GetTableName(Me.Table)

            '        End If
            '    End Get
            'End Property


            Public Sub New()

            End Sub

            Public Sub FillFields()

                'DB.GetTableName()

                Me.Fields = DBase.scafolding.Fields.GetFieldsSql(Me.SqlView)

            End Sub

            Public Function GetPrimary() As String()

                Return (From f In Fields Where f.Primary = True Select f.Name).ToArray
                'Return Fields.Where(Function(o) o.primary = True).Select(Function(o) o.name).ToArray   
            End Function

        End Class

        Public Class Field
            Public Property Name As String
            Public Property LabelName As String
            Public Property [ReadOnly] As Boolean
            Public Property Primary As Boolean
            Public Property ConfigTable As ConfigTable
            Public Property SqlFieldDescription As String
            Public Property SqlFieldID As String
            Public Property Check As Boolean
            Public Property InputType As ConfigTable.InputType
            Public Property Multiline As Boolean
            Public Property Rows As Integer
            Public Property NotNull As Boolean
            Public Property RegulaExpression As RegulaExpressionType
            Public Property RECod As String
            Public Property ShowInLine As Boolean = False
            Public Property ShowOutLine As Boolean = False

            Public Property FieldType() As Type

            Private _ControlName As String
            Public ReadOnly Property ControlName() As String

                Get
                    Return GetControlName(Me.InputType, Me.Name)
                End Get

            End Property

            Public Shared Function GetControlName(InputType As ConfigTable.InputType, FieldName As String) As String

                Select Case InputType
                    Case ConfigTable.InputType.DropDownList
                        Return "Ddl_" & FieldName

                    Case ConfigTable.InputType.CheckBox
                        Return "Chb_" & FieldName

                    Case ConfigTable.InputType.[Default]
                        Return "Txt_" & FieldName

                    Case ConfigTable.InputType.SearchBox
                        Return "Scr_" & FieldName

                    Case ConfigTable.InputType.SearchView
                        Return "View_" & FieldName

                    Case ConfigTable.InputType.Date
                        Return "DataTime" & FieldName

                End Select
                Return 0
            End Function

        End Class

        Public Class TypeEdit

            Public Enum StatusType
                [ReadOnly]
                Insert
                Update
                Delete
            End Enum

        End Class

        Public Class RegulaExpression

            Public Enum RegulaExpressionType
                numeric
                numeric_1_999
                email

            End Enum

            Public Shared Function GetRegulaExpression(Type As RegulaExpressionType) As String

                Select Case Type

                    Case RegulaExpressionType.numeric
                        Return "\b\d+\b"

                    Case RegulaExpressionType.numeric_1_999
                        Return "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"

                    Case RegulaExpressionType.email
                        Return "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"

                End Select
                Return 0
            End Function

        End Class
    End Namespace

    Namespace Grid

        Public Class myTemplate_TB
            Inherits TemplateField
            Public Sub New(f As Field)

                Me.SortExpression = f.Name
                Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_Txb(GridViewTemplate_Txb.RowType.Edit, f)
                End If
                Me.ItemTemplate = New GridViewTemplate_Txb(GridViewTemplate_Txb.RowType.Item, f)
                'Me.HeaderTemplate = New GridViewTemplate_Txb(GridViewTemplate_Txb.RowType.Header, f)

            End Sub
        End Class

        Public Class myTemplate_DL
            Inherits TemplateField

            Public Sub New(db As DBContext, f As Field)

                'Me.SortExpression = f.Name
                'Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_Ddl(db, GridViewTemplate_Ddl.RowType.Edit, f)
                End If

                Me.ItemTemplate = New GridViewTemplate_Ddl(db, GridViewTemplate_Ddl.RowType.Item, f)
                Me.HeaderTemplate = New GridViewTemplate_Ddl(db, GridViewTemplate_Ddl.RowType.Header, f)

            End Sub

        End Class

        Public Class myTemplate_CB
            Inherits TemplateField

            Public Sub New(f As Field)

                Me.SortExpression = f.Name
                Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_Chb(GridViewTemplate_Chb.RowType.Edit, f)
                End If

                Me.ItemTemplate = New GridViewTemplate_Chb(GridViewTemplate_Chb.RowType.Item, f)
                'Me.HeaderTemplate = New GridViewTemplate_Chb(GridViewTemplate_Chb.RowType.Header, f)

            End Sub

        End Class

        Public Class myTemplate_SC
            Inherits TemplateField

            Public Sub New(db As DBContext, f As Field)

                'Me.SortExpression = f.SqlFieldDescription
                'Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_Scr(db, GridViewTemplate_Scr.RowType.Edit, f)
                End If

                Me.ItemTemplate = New GridViewTemplate_Scr(db, GridViewTemplate_Scr.RowType.Item, f)
                Me.HeaderTemplate = New GridViewTemplate_Scr(db, GridViewTemplate_Scr.RowType.Header, f)

            End Sub

        End Class

        Public Class myTemplate_SV
            Inherits TemplateField

            Public Sub New(db As DBContext, f As Field)

                'Me.SortExpression = f.SqlFieldDescription
                'Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_ScView(db, GridViewTemplate_ScView.RowType.Edit, f)
                End If

                Me.ItemTemplate = New GridViewTemplate_ScView(db, GridViewTemplate_ScView.RowType.Item, f)
                Me.HeaderTemplate = New GridViewTemplate_ScView(db, GridViewTemplate_ScView.RowType.Header, f)

            End Sub

        End Class

        Public Class myTemplate_Data
            Inherits TemplateField

            Public Sub New(db As DBContext, f As Field)

                'Me.SortExpression = f.SqlFieldDescription
                'Me.HeaderText = f.LabelName

                If f.ReadOnly = False Then
                    Me.EditItemTemplate = New GridViewTemplate_Data(db, GridViewTemplate_Data.RowType.Edit, f)
                End If

                Me.ItemTemplate = New GridViewTemplate_Data(db, GridViewTemplate_Data.RowType.Item, f)
                Me.HeaderTemplate = New GridViewTemplate_Data(db, GridViewTemplate_Data.RowType.Header, f)

            End Sub

        End Class

        Public Class GridViewTemplate_Txb
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field

            Sub New(ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn
                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)
                        Dim ll = New LinkButtonControlBuilder


                    Case RowType.Item
                        Dim Lbl As New Label
                        Lbl.CssClass = "inline_edit_input"
                        AddHandler Lbl.DataBinding, AddressOf DataBinding_label
                        Container.Controls.Add(Lbl)

                    Case RowType.Edit
                        Dim Txt As New TextBox
                        Txt.CssClass = "inline_edit_input"
                        Txt.ID = Field.GetControlName(ConfigTable.InputType.[Default], f.Name)
                        AddHandler Txt.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Txt)

                        If f.Multiline Then
                            Txt.TextMode = TextBoxMode.MultiLine
                            Txt.Rows = f.Rows
                        End If

                        If f.NotNull Then
                            'Container.Controls.Add(New LiteralControl("<br />"))
                            Dim Validator As New RequiredFieldValidator
                            Validator.CssClass = "inline_edit_validator"
                            Validator.ForeColor = Drawing.Color.Red
                            Validator.ControlToValidate = Field.GetControlName(ConfigTable.InputType.[Default], f.Name)
                            Validator.Text = "Inserisci Valore"
                            Validator.ValidationGroup = "ValidatorGroupRow"
                            Container.Controls.Add(Validator)
                        End If

                        If f.RegulaExpression Then
                            'Container.Controls.Add(New LiteralControl("<br />"))
                            Dim Validator As New RegularExpressionValidator
                            Validator.CssClass = "inline_regularexpression"
                            Validator.ValidationExpression = RegulaExpression.GetRegulaExpression(f.RegulaExpression)
                            Validator.ForeColor = Drawing.Color.Red
                            Validator.ControlToValidate = Field.GetControlName(ConfigTable.InputType.[Default], f.Name)
                            Validator.Text = "Valore Errato"
                            Validator.ValidationGroup = "ValidatorGroupRow"
                            Container.Controls.Add(Validator)
                        End If

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Txt As TextBox = CType(sender, TextBox)
                Dim Row As GridViewRow = CType(Txt.NamingContainer, GridViewRow)
                Txt.Text = DataBinder.Eval(Row.DataItem, f.Name).ToString()

            End Sub

            Private Sub DataBinding_label(ByVal sender As Object, ByVal e As EventArgs)

                Dim Lbl As Label = CType(sender, Label)
                Dim Row As GridViewRow = CType(Lbl.NamingContainer, GridViewRow)
                Lbl.Text = DataBinder.Eval(Row.DataItem, f.Name).ToString()
            End Sub

        End Class

        Public Class GridViewTemplate_Ddl
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field
            Private DB As DBContext

            Sub New(db As DBContext, ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
                Me.DB = db
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)

                    Case RowType.Item
                        Dim Lbl As New Label
                        Lbl.CssClass = "inline_edit_input"
                        AddHandler Lbl.DataBinding, AddressOf DataBinding_label
                        Container.Controls.Add(Lbl)

                    Case RowType.Edit
                        Dim Ddl As New DropDownList
                        Ddl.CssClass = "inline_edit_input"
                        Ddl.ID = Field.GetControlName(ConfigTable.InputType.DropDownList, f.Name)
                        AddHandler Ddl.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Ddl)

                        If f.NotNull Then
                            'Container.Controls.Add(New LiteralControl("<br />"))
                            Dim Validator As New RequiredFieldValidator
                            Validator.CssClass = "inline_edit_validator"
                            Validator.ForeColor = Drawing.Color.Red
                            Validator.ControlToValidate = Field.GetControlName(ConfigTable.InputType.DropDownList, f.Name)
                            Validator.Text = "Inserisci Valore"
                            Validator.ValidationGroup = "ValidatorGroupRow"
                            Container.Controls.Add(Validator)
                        End If

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Ddl As DropDownList = CType(sender, DropDownList)
                Dim Row As GridViewRow = CType(Ddl.NamingContainer, GridViewRow)

                Dim DT As DataTable
                DT = f.ConfigTable.SqlView.ToDataTable
                'Ddl.Items.Insert(0, New ListItem(f.SqlFieldDescription, ""))
                Ddl.Items.Insert(0, New ListItem("-Select-", ""))
                For count = 0 To DT.Rows.Count - 1
                    Ddl.Items.Add(New ListItem(DT.Rows(count)(f.SqlFieldDescription), DT.Rows(count)(f.SqlFieldID)))
                Next

                If Ddl.Items.FindByValue(DataBinder.Eval(Row.DataItem, f.Name).ToString()) IsNot Nothing Then
                    Ddl.SelectedValue = DataBinder.Eval(Row.DataItem, f.Name).ToString()
                End If

            End Sub

            Private Sub DataBinding_label(ByVal sender As Object, ByVal e As EventArgs)

                Dim Lbl As Label = CType(sender, Label)
                Dim Row As GridViewRow = CType(Lbl.NamingContainer, GridViewRow)
                Lbl.Text = DataBinder.Eval(Row.DataItem, f.Name).ToString()

                Dim DT As DataTable

                Dim q = f.ConfigTable.SqlView.Clone

                DT = q.Where(f.ConfigTable.TableName & "." & f.SqlFieldID, "=", Lbl.Text).ToDataTable
                If DT.Rows.Count > 0 Then
                    Lbl.Text = DT.Rows(0)(f.SqlFieldDescription).ToString
                Else
                    Lbl.Text = ""
                End If

            End Sub
        End Class

        Public Class GridViewTemplate_Chb
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field

            Sub New(ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)

                    Case RowType.Item
                        Dim val As New CheckBox
                        val.CssClass = "inline_edit_input"
                        AddHandler val.DataBinding, AddressOf DataBinding_label
                        Container.Controls.Add(val)

                    Case RowType.Edit

                        Dim Chb As New CheckBox
                        Chb.CssClass = "inline_edit_input"
                        Chb.ID = Field.GetControlName(ConfigTable.InputType.CheckBox, f.Name)
                        AddHandler Chb.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Chb)

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Chb As CheckBox = CType(sender, CheckBox)
                Dim Row As GridViewRow = CType(Chb.NamingContainer, GridViewRow)
                Chb.Checked = CBool(Utils.Conv.ToInt(DataBinder.Eval(Row.DataItem, f.Name)))

            End Sub

            Private Sub DataBinding_label(ByVal sender As Object, ByVal e As EventArgs)

                Dim Chb As CheckBox = CType(sender, CheckBox)
                Dim Row As GridViewRow = CType(Chb.NamingContainer, GridViewRow)
                'Chb.Checked = Utils.Conv.ToBool(DataBinder.Eval(Row.DataItem, f.Name))
                Chb.Checked = IIf(DataBinder.Eval(Row.DataItem, f.Name) = 0, False, True)
                Chb.Enabled = False

            End Sub
        End Class

        Public Class GridViewTemplate_Scr
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field
            Private DB As DBContext

            Sub New(db As DBContext, ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
                Me.DB = db
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)

                    Case RowType.Item
                        Dim Lbl As New Label
                        Lbl.CssClass = "inline_edit_input"
                        AddHandler Lbl.DataBinding, AddressOf DataBinding_label
                        Container.Controls.Add(Lbl)

                    Case RowType.Edit
                        Dim Search As New DBase.scafolding.CustomControlSearch
                        Search.CssClass = "inline_edit_input"
                        Search.ID = Field.GetControlName(ConfigTable.InputType.SearchBox, f.Name)
                        Search.field = Me.f
                        AddHandler Search.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Search)

                        'If f.NotNull Then
                        '    Container.Controls.Add(New LiteralControl("<br />"))
                        '    'Dim aaa As ServerValidateEventArgs
                        '    'aaa.Value = ""
                        '    Dim Validator As New CustomValidator
                        '    Validator.ForeColor = Drawing.Color.Red
                        '    Validator.ControlToValidate = Field.GetControlName(ConfigTable.InputType.SearchBox, f.Name)
                        '    Validator.Text = "Inserisci Valore"
                        '    Validator.ValidationGroup = "ValidatorGroupRow"
                        '    'Validator.ClientValidationFunction = ControllaData(Me, aaa)
                        '    Validator.IsValid = ControllaData(Search.Value)
                        '    'AddHandler Validator.ServerValidate, AddressOf ControllaData
                        '    Container.Controls.Add(Validator)
                        'End If

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_label(ByVal sender As Object, ByVal e As EventArgs)

                Dim Lbl As Label = CType(sender, Label)
                Dim Search As New DBase.scafolding.CustomControlSearch
                Search.ID = Field.GetControlName(ConfigTable.InputType.SearchBox, f.Name)
                Search.field = f
                Dim Row As GridViewRow = CType(Lbl.NamingContainer, GridViewRow)
                Search.Value = DataBinder.Eval(Row.DataItem, f.Name).ToString
                Lbl.Text = Search.Description

            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Search As DBase.scafolding.CustomControlSearch = CType(sender, DBase.scafolding.CustomControlSearch)
                Dim Row As GridViewRow = CType(Search.NamingContainer, GridViewRow)
                Search.Value = DataBinder.Eval(Row.DataItem, f.Name).ToString

                Dim q = f.ConfigTable.SqlView.Clone
                Dim DT As DataTable
                If Search.Value <> "" Then
                    DT = q.Where(f.SqlFieldID, "=", Search.Value).ToDataTable
                    If DT.Rows.Count > 0 Then
                        Search.Value = DT.Rows(0)(f.SqlFieldID).ToString()
                    End If
                End If

            End Sub
        End Class

        Public Class GridViewTemplate_ScView
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field
            Private DB As DBContext

            Sub New(db As DBContext, ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
                Me.DB = db
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)

                    Case RowType.Edit, RowType.Item
                        Dim Search As New DBase.scafolding.CustomControlView
                        Search.CssClass = "inline_edit_input"
                        Search.field = Me.f
                        AddHandler Search.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Search)

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Search As DBase.scafolding.CustomControlView = CType(sender, DBase.scafolding.CustomControlView)
                Dim Row As GridViewRow = CType(Search.NamingContainer, GridViewRow)
                Search.filtro = Utils.Conv.ToInt((DataBinder.Eval(Row.DataItem, f.Name).ToString))

            End Sub
        End Class

        Public Class GridViewTemplate_Data
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Public f As Field
            Private DB As DBContext

            Sub New(db As DBContext, ByVal Type As RowType, f As Field)
                Me.f = f
                Me.TemplateType = Type
                Me.DB = db
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType

                    Case RowType.Header
                        Dim Lab As New Label
                        Lab.CssClass = "inline_edit_label"
                        Lab.Text = f.LabelName
                        Container.Controls.Add(Lab)

                    Case RowType.Item
                        Dim Lbl As New Label
                        Lbl.CssClass = "inline_edit_input"
                        AddHandler Lbl.DataBinding, AddressOf DataBinding_label
                        Container.Controls.Add(Lbl)

                    Case RowType.Edit
                        Dim Calendar As New DBase.scafolding.CustomControData
                        Calendar.CssClass = "inline_edit_input"
                        Calendar.ID = Field.GetControlName(ConfigTable.InputType.Date, f.Name)
                        AddHandler Calendar.DataBinding, AddressOf DataBinding_text
                        Container.Controls.Add(Calendar)

                    Case Else
                End Select
            End Sub

            Private Sub DataBinding_label(ByVal sender As Object, ByVal e As EventArgs)

                Dim Lbl As Label = CType(sender, Label)
                Dim Calendar As New DBase.scafolding.CustomControData
                Calendar.ID = Field.GetControlName(ConfigTable.InputType.Date, f.Name)
                Dim Row As GridViewRow = CType(Lbl.NamingContainer, GridViewRow)
                Calendar.Value = DataBinder.Eval(Row.DataItem, f.Name).ToString
                Dim dd As Date
                If Date.TryParse(Calendar.Value, dd) Then
                    Lbl.Text = Calendar.Value
                End If
            End Sub

            Private Sub DataBinding_text(ByVal sender As Object, ByVal e As EventArgs)

                Dim Calendar As DBase.scafolding.CustomControData = CType(sender, DBase.scafolding.CustomControData)
                Dim Row As GridViewRow = CType(Calendar.NamingContainer, GridViewRow)
                Dim dd As Date
                If Date.TryParse(DataBinder.Eval(Row.DataItem, f.Name).ToString, dd) Then
                    'Calendar.Value = DateSerial(dd.Year, dd.Month, dd.Day)
                    Calendar.Value = New Date(dd.Year, dd.Month, dd.Day, dd.Hour, dd.Minute, dd.Second)
                End If
            End Sub
        End Class

        Public Class myTemplateGridInLine
            Inherits TemplateField

            Public Sub New()
                Me.EditItemTemplate = New GridViewTemplateInLine(GridViewTemplateInLine.RowType.Edit)
                Me.ItemTemplate = New GridViewTemplateInLine(GridViewTemplateInLine.RowType.Item)
                Me.HeaderTemplate = New GridViewTemplateInLine(GridViewTemplateInLine.RowType.Header)
            End Sub

        End Class

        Public Class GridViewTemplateInLine
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit
            End Enum

            Private TemplateType As RowType
            Sub New(ByVal Type As RowType)
                Me.TemplateType = Type
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                Select Case TemplateType
                    Case RowType.Item
                        Dim LinkBtnEdit As New LinkButton
                        LinkBtnEdit.CssClass = "LinkButton"
                        LinkBtnEdit.CausesValidation = False
                        LinkBtnEdit.CommandName = DBase.scafolding.Fields.rowCommand.Edit.ToString
                        LinkBtnEdit.Text = DBase.scafolding.Fields.rowCommand.Edit.ToString
                        Container.Controls.Add(LinkBtnEdit)

                        Dim LinkBtnDelete As New LinkButton
                        LinkBtnDelete.CssClass = "LinkButton"
                        LinkBtnDelete.CausesValidation = False
                        LinkBtnDelete.CommandName = DBase.scafolding.Fields.rowCommand.Delete.ToString
                        LinkBtnDelete.Text = DBase.scafolding.Fields.rowCommand.Delete.ToString
                        LinkBtnDelete.OnClientClick = "return confirm('Sei sicuro di voler eliminare il record?');"
                        Container.Controls.Add(LinkBtnDelete)

                    Case RowType.Edit
                        Dim LinkBtnUpdate As New LinkButton
                        LinkBtnUpdate.ID = "LinkBtnUpdate"
                        LinkBtnUpdate.CssClass = "LinkButton"
                        LinkBtnUpdate.CausesValidation = True
                        LinkBtnUpdate.CommandName = DBase.scafolding.Fields.rowCommand.Update.ToString
                        LinkBtnUpdate.Text = DBase.scafolding.Fields.rowCommand.Update.ToString
                        LinkBtnUpdate.ValidationGroup = "ValidatorGroupRow"
                        Container.Controls.Add(LinkBtnUpdate)

                        Dim LinkBtnCancel As New LinkButton
                        LinkBtnCancel.CssClass = "LinkButton"
                        LinkBtnCancel.CausesValidation = False
                        LinkBtnCancel.CommandName = DBase.scafolding.Fields.rowCommand.Cancel.ToString
                        LinkBtnCancel.Text = DBase.scafolding.Fields.rowCommand.Cancel.ToString
                        Container.Controls.Add(LinkBtnCancel)

                    Case Else
                End Select
            End Sub
        End Class

        Public Class myTemplateGridSelect
            Inherits TemplateField

            Public Sub New()
                Me.EditItemTemplate = New GridViewTemplateSelect(GridViewTemplateSelect.RowType.Edit)
                Me.ItemTemplate = New GridViewTemplateSelect(GridViewTemplateSelect.RowType.Item)
                Me.HeaderTemplate = New GridViewTemplateSelect(GridViewTemplateSelect.RowType.Header)
            End Sub

        End Class

        Public Class GridViewTemplateSelect
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit
            End Enum

            Private TemplateType As RowType
            Sub New(ByVal Type As RowType)
                Me.TemplateType = Type
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                If TemplateType = RowType.Item Or TemplateType = RowType.Edit Then
                    Dim BtnSelect As New Button
                    BtnSelect.CssClass = "Button"
                    BtnSelect.CausesValidation = False
                    BtnSelect.CommandName = DBase.scafolding.Fields.rowCommand.Select.ToString
                    BtnSelect.Text = DBase.scafolding.Fields.rowCommand.Select.ToString
                    Container.Controls.Add(BtnSelect)
                End If
            End Sub
        End Class

        Public Class myTemplateGridOutLine
            Inherits TemplateField

            Public Sub New()

                Me.EditItemTemplate = New GridViewTemplateOutLine(GridViewTemplateOutLine.RowType.Edit)
                Me.ItemTemplate = New GridViewTemplateOutLine(GridViewTemplateOutLine.RowType.Item)
                Me.HeaderTemplate = New GridViewTemplateOutLine(GridViewTemplateOutLine.RowType.Header)

            End Sub

        End Class

        Public Class GridViewTemplateOutLine
            Implements ITemplate

            Public Enum RowType
                Header
                Item
                Footer
                Edit

            End Enum

            Private TemplateType As RowType
            Sub New(ByVal Type As RowType)
                Me.TemplateType = Type
            End Sub

            Sub InstantiateIn(ByVal Container As System.Web.UI.Control) _
              Implements ITemplate.InstantiateIn

                If TemplateType = RowType.Item Or TemplateType = RowType.Edit Then

                    Dim BtnEdit As New Button
                    BtnEdit.CssClass = "Button"
                    BtnEdit.CommandName = DBase.scafolding.Fields.rowCommand.EditOutLine.ToString
                    BtnEdit.Text = DBase.scafolding.Fields.rowCommand.EditOutLine.ToString
                    Container.Controls.Add(BtnEdit)

                    Dim BtnDelete As New Button
                    BtnDelete.CssClass = "Button"
                    BtnDelete.CommandName = DBase.scafolding.Fields.rowCommand.DeleteOutLine.ToString
                    BtnDelete.Text = DBase.scafolding.Fields.rowCommand.DeleteOutLine.ToString
                    Container.Controls.Add(BtnDelete)

                End If

            End Sub
        End Class

    End Namespace

    Public Class Fields

        Public Shared Function GetDataRow(db As DBContext, id As System.Collections.Generic.Dictionary(Of String, Object), tab As MySql.SqlQuery(Of Object)) As DataRow

            Dim dt As DataTable
            Dim dr As DataRow

            Dim q = tab.Clone

            For Each y In id
                q.Where(y.Key, "=", y.Value)
            Next
            dt = q.ToDataTable

            If dt.Rows.Count <> 0 Then
                dr = dt(0)
                Return dr
            End If

            Return (Nothing)
        End Function

        Public Shared Function GetFields(db As DBContext, table As String) As List(Of Field)

            Dim q = db.GetQuery
            Dim pp As New ConfigTable
            Dim DTField As DataTable
            DTField = q.Sql("DESCRIBE " & table).ToDataTable

            For count = 0 To DTField.Rows.Count - 1

                Dim Label As String
                Dim Type As String
                Dim IsPrimary As Boolean

                Label = DTField.Rows(count)(0)

                If DTField.Rows(count)(3) = "PRI" Then
                    IsPrimary = True
                    Type = ConfigTable.InputType.[Default]
                Else
                    IsPrimary = False
                    Type = ConfigTable.InputType.[Default]
                End If

                pp.Fields.Add(New Field With {
                    .Name = Label,
                    .LabelName = Label & "_descr",
                    .Primary = IsPrimary,
                    .[ReadOnly] = IsPrimary,
                    .InputType = Type,
                    .ShowInLine = True,
                    .ShowOutLine = True
                })

            Next

            Return pp.Fields

        End Function

        Public Shared Function GetFieldsSql(q As MySql.SqlQuery(Of Object), ColumnNameFiltro As String) As List(Of Field)

            'Dim q = db.GetQuery
            Dim Fields As New List(Of Field)
            Dim DTField As DataTable



            DTField = q.Where("1", "<>", 1).ToDataTable

            If DTField.Columns.Count > 0 Then


                Fields.Add(New Field With {.Name = DTField.Columns(0).ColumnName,
                                              .LabelName = DTField.Columns(0).ColumnName,
                                              .Primary = True,
                                              .[ReadOnly] = True,
                                              .InputType = ConfigTable.InputType.[Default],
                                              .ShowInLine = True})
            End If

            For count = 1 To DTField.Columns.Count - 1
                If DTField.Columns(count).ColumnName <> ColumnNameFiltro Then

                    Fields.Add(New Field With {.Name = DTField.Columns(count).ColumnName,
                                          .LabelName = DTField.Columns(count).ColumnName,
                                          .Primary = False,
                                          .[ReadOnly] = False,
                                          .InputType = ConfigTable.InputType.[Default],
                                          .ShowInLine = True,
                                          .NotNull = True})
                Else
                    Fields.Add(New Field With {.Name = DTField.Columns(count).ColumnName,
                                          .LabelName = DTField.Columns(count).ColumnName,
                                          .Primary = False,
                                          .[ReadOnly] = True,
                                          .InputType = ConfigTable.InputType.[Default],
                                          .ShowInLine = True,
                                          .NotNull = False})
                End If
            Next

            Return Fields

        End Function

        Public Shared Function GetFieldsSql2(db As DBContext, sql As String, ColumnNameFiltro As String, list_PKs As List(Of String), list_fields As List(Of String)) As List(Of Field)

            Dim q = db.GetQuery
            Dim Fields As New List(Of Field)
            Dim DTField As DataTable

            DTField = q.Sql(sql).Where("1", "<>", 1).ToDataTable

            For Each pk In list_PKs
                If pk = ColumnNameFiltro Then

                    Fields.Add(New Field With {.Name = pk,
                                                .LabelName = pk,
                                                .Primary = True,
                                                .[ReadOnly] = True,
                                                .InputType = ConfigTable.InputType.[Default],
                                                .ShowInLine = False})

                Else
                    Fields.Add(New Field With {.Name = pk,
                                                .LabelName = pk,
                                                .Primary = True,
                                                .[ReadOnly] = False,
                                                .InputType = ConfigTable.InputType.[Default],
                                                .ShowInLine = True})
                End If
            Next

            For Each field In list_fields
                Fields.Add(New Field With {.Name = field,
                                            .LabelName = field,
                                            .Primary = False,
                                            .[ReadOnly] = True,
                                            .InputType = ConfigTable.InputType.[Default],
                                            .ShowInLine = True})
            Next

            Return Fields

        End Function

        Public Shared Function GetFieldsSql(q As MySql.SqlQuery(Of Object)) As List(Of Field)

            Return GetFieldsSql(q, "")

        End Function

        Public Shared Function GetLastID(db As DBContext) As UInteger

            Dim q = db.GetQuery
            Return q.Sql("SELECT LAST_INSERT_ID()").ToObj(Of UInteger)()

        End Function

        Public Enum rowCommand

            Edit
            Delete
            Update
            Cancel
            [Select]
            EditOutLine
            DeleteOutLine

        End Enum

        Public Enum filter

            [AND]
            [OR]

        End Enum

        Public Shared Function GetConv(type As Type, s As String) As Object

            Select Case type
                Case GetType(String)
                    Return s
                Case GetType(Integer)
                    Return Utils.Conv.ToInt(s)
                Case GetType(Double)
                    Return Utils.Conv.ToDouble(s)
                Case GetType(Date)
                    Return Utils.Conv.ToDate(s)
                Case Else
                    Return s
            End Select

        End Function

    End Class
End Namespace
